package com.sh.mqtt;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.eclipse.paho.client.mqttv3.MqttClient;
import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
import org.eclipse.paho.client.mqttv3.MqttDeliveryToken;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.eclipse.paho.client.mqttv3.MqttMessage;
import org.eclipse.paho.client.mqttv3.MqttPersistenceException;
import org.eclipse.paho.client.mqttv3.MqttTopic;
import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.sh.controller.TestController;
import com.sh.util.SysConfig;

/**
 * @comments 服务器向多个客户端推送主题，即不同客户端可向服务器订阅相同主题
 * @author zg
 * @date 2018年3月14日
 */
//@Component
public class ShMqttClient {
	private static Logger logger = LogManager.getLogger(TestController.class);
	
	/*public static final String HOST = "tcp://127.0.0.1:61613";
	public static final String TOPIC = "toclient/124";
	public static final String TOPIC125 = "toclient/125";
	private static final String clientid = "server";*/

	private SysConfig conf;
	
	private MqttClient client;
	private MqttTopic topic;
	private MqttTopic topic125;
	private String userName = "admin";
	private String passWord = "password";

	private MqttMessage message;

	@Autowired
	public ShMqttClient(SysConfig conf) throws MqttException {
		this.conf = conf;
		String host = conf.getMqttHost();
		String clientId = conf.getMqttClientId();
		logger.info("Client（{}）开始连接MQ（{}）...", client, host);
		// MemoryPersistence设置clientid的保存形式，默认为以内存保存
		client = new MqttClient(host, clientId, new MemoryPersistence());
		connect();
	}
	
	

	private void connect() {
		MqttConnectOptions options = new MqttConnectOptions();
		options.setCleanSession(false);
		options.setUserName(userName);
		options.setPassword(passWord.toCharArray());
		// 设置超时时间
		options.setConnectionTimeout(10);
		// 设置会话心跳时间
		options.setKeepAliveInterval(20);
		try {
			client.setCallback(new PushCallback());
			client.connect(options);
			//topic = client.getTopic(TOPIC);
			//topic125 = client.getTopic(TOPIC125);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void publish(MqttTopic topic, MqttMessage message) throws MqttPersistenceException, MqttException {
		MqttDeliveryToken token = topic.publish(message);
		token.waitForCompletion();
		System.out.println("message is published completely! " + token.isComplete());
	}

	/*public static void main(String[] args) throws MqttException {
		ShMqttClient server = new ShMqttClient();

		server.message = new MqttMessage();
		server.message.setQos(2);
		server.message.setRetained(true);
		server.message.setPayload("给客户端124推送的信息".getBytes());
		server.publish(server.topic, server.message);

		server.message = new MqttMessage();
		server.message.setQos(2);
		server.message.setRetained(true);
		server.message.setPayload("给客户端125推送的信息".getBytes());
		server.publish(server.topic125, server.message);

		System.out.println(server.message.isRetained() + "------ratained状态");
	}*/
}
